package com.wwwc.admin.web.database;

import java.io.*;
import java.util.*;
import java.sql.SQLException;

public class DatabaseGenerateQuery {

    public StringBuffer getInsertQuery(String table_name, Vector vh, Vector vt, Vector vr)
    {
	String qhead = "INSERT INTO "+table_name+ "(";
	Enumeration enumh = vh.elements();

	while (enumh.hasMoreElements()) {
	    qhead += (String)enumh.nextElement()+",";
	}

	qhead = qhead.substring(0, qhead.length()-1)+") VALUES (";

	String tempv = null;
	String tempt = null;
	Vector v = null;

	StringBuffer qsbf = new StringBuffer();

	Enumeration enumr = vr.elements();
	while (enumr.hasMoreElements()) {
	    StringBuffer sbf = new StringBuffer(qhead);
	    v = (Vector)enumr.nextElement();
	    if(v.size() != vh.size()) {
		qsbf.append("Error: Two table columns not match!");
		return qsbf;
	    }

	    for(int m=0; m<vh.size(); m++) {
		tempt = (String)vt.elementAt(m);
		if(tempt.startsWith("VARCHAR") || tempt.equals("CHAR") || tempt.equals("LONGCHAR") || tempt.equals("DATE")){
		    tempv = (String)v.elementAt(m);
		    if(tempv != null && tempv.indexOf("'") != -1) {
			tempv = tempv.replaceAll("'", "''");
		    }

		    if(m != (vh.size()-1)) {
			sbf.append("'"+tempv+"',");
		    }
		    else {
			sbf.append("'"+tempv+"');");
		    }
		}
		else if(tempt.equals("INTEGER") || tempt.equals("DOUBLE") || 
		   tempt.equals("NUMERIC") || tempt.equals("BIGINT") ||
		   tempt.equals("SMALLINT")){
		    if(m != (vh.size()-1)) {
			sbf.append(v.elementAt(m)+",");
		    }
		    else {
			sbf.append(v.elementAt(m)+");");
		    }
		}
		else {
		    System.out.println("DatabaseGenerateQuery:Error:[Data type:"+tempt+" not found]");
		    return null;
		}
	    }

	    qsbf.append(sbf);
	}
	return qsbf;
    }

    public StringBuffer getInsertQuery(String table_name, String new_table_name, Vector vh, Vector vt, Vector vr)
    {
	String qhead = "INSERT INTO "+new_table_name+ "(";
	Enumeration enumh = vh.elements();

	while (enumh.hasMoreElements()) {
	    qhead += (String)enumh.nextElement()+",";
	}

	qhead = qhead.substring(0, qhead.length()-1)+") VALUES (";

	String tempv = null;
	String tempt = null;
	Vector v = null;

	StringBuffer qsbf = new StringBuffer();

	Enumeration enumr = vr.elements();
	while (enumr.hasMoreElements()) {
	    StringBuffer sbf = new StringBuffer(qhead);
	    v = (Vector)enumr.nextElement();
	    if(v.size() != vh.size()) {
		qsbf.append("Error: Two table columns not match!");
		return qsbf;
	    }

	    for(int m=0; m<vh.size(); m++) {
		tempt = (String)vt.elementAt(m);
		if(tempt.startsWith("VARCHAR") || tempt.equals("CHAR") || tempt.equals("LONGCHAR")){
		    tempv = (String)v.elementAt(m);
		    if(tempv != null && tempv.indexOf("'") != -1) {
			tempv = tempv.replaceAll("'", "''");
		    }

		    if(m != (vh.size()-1)) {
			sbf.append("'"+tempv+"',");
		    }
		    else {
			sbf.append("'"+tempv+"');");
		    }
		}
		else if(tempt.equals("INTEGER") || tempt.equals("DOUBLE") || 
		   tempt.equals("NUMERIC") || tempt.equals("BIGINT") ||
		   tempt.equals("SMALLINT")){
		    if(m != (vh.size()-1)) {
			sbf.append(v.elementAt(m)+",");
		    }
		    else {
			sbf.append(v.elementAt(m)+");");
		    }
		}
		else {
		    sbf = new StringBuffer();
		    sbf.append("DatabaseGenerateQuery:Error:[Data type:"+tempt+" not found]");
		    return sbf;
		}
	    }

	    qsbf.append(sbf);
	}
	return qsbf;
    }
}








